package com.wdz.service.impl;



import com.wdz.entity.ApiUser;
import com.wdz.http.Response;
import com.wdz.fegin.FeginAdminService;
import com.wdz.utils.SecurityModelFactory;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class UserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    private FeginAdminService feginAdminService;




    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

        Response response = feginAdminService.getApiUser(username);
        ApiUser user = new ApiUser();
        Map<String,Object> map = (Map<String, Object>) response.getData();
        user.setId((String) map.get("id"));
        user.setUsername((String) map.get("username"));
        user.setPassword((String) map.get("password"));
        user.setAuthorities((String) map.get("authorities"));
        if (user == null) {
            throw new UsernameNotFoundException(String.format("No user found with username '%s'.", username));
        } else {
            return SecurityModelFactory.create(user);
        }
    }
}


